//
//  basePopView.swift
//  LeDian
//
//  Created by 三秒文化 on 16/4/1.
//  Copyright © 2016年 51ledian. All rights reserved.
//

import UIKit

class popActionSheetView: UIView {
    
    var originSize:CGSize! = CGSizeZero
    let SampleTag = 777
    var btnH:CGFloat = 45
    var cancelBtnH:CGFloat = 50
    var marginBottom:CGFloat = 5
    //title数组
    var selectSheetTextArr:Array<String>? {
        didSet{
            creatSelectBtn()
        }
    }

    var cancelBtn:UIButton?
    var showHeadView:Bool = false
    var headViewH:CGFloat = 30
    var headTitle:String?
    
    var tapClosure:((title:String,index:Int) ->Void)?
    
    lazy var gampView:UIView? = {
        let gampview = UIView.init(frame: LDFrame.ScreenBounds)
        gampview.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.4)
        gampview.userInteractionEnabled = true
        gampview.bk_whenTapped({ [weak self] in
            self?.dismissview()
            })
        return gampview
        
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.frame = CGRectMake(0, LDFrame.ScreenHeight,LDFrame.ScreenWidth, 0)
        backgroundColor = LDGolbalColor.BgGoundGray
        layer.cornerRadius = 3
        creatview(frame)
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func creatview(frame:CGRect) {
        cancelBtn = UIButton.setDetailsBtn("取消", picName: nil, btnBackColor: LDGolbalColor.White, titleColor: LDFontColor.Red)
        cancelBtn?.titleLabel?.font = LDFont.Font15
        cancelBtn?.addTarget(self, action: #selector(dismissview), forControlEvents: .TouchUpInside)
        addSubview(cancelBtn!)
        
    }
    //根据数组个数创建
    func creatSelectBtn() {
        
        let count = selectSheetTextArr!.count
        for i in 0..<count{
            
            let secBtn = UIButton.setDetailsBtn(selectSheetTextArr![i], picName: nil, btnBackColor: LDGolbalColor.White, titleColor: LDFontColor.DeepGray)
            secBtn?.layer.borderWidth = 0.5
            secBtn?.layer.borderColor = LDIconColor.LightGray.CGColor
            secBtn?.bk_whenTapped({ [weak self] in
                if self!.tapClosure != nil {
                    self?.tapClosure!(title: self!.selectSheetTextArr![i],index: i+1)
                    self?.dismissview()
                }
                })
            secBtn?.frame = CGRectMake(0, btnH * CGFloat(i), LDFrame.ScreenWidth, btnH)
            
            secBtn?.tag = SampleTag + i
            addSubview(secBtn!)
        }
        cancelBtn?.frame = CGRectMake(0, CGFloat(count)*btnH + marginBottom, LDFrame.ScreenWidth, cancelBtnH)
        originSize.height = CGFloat(count)*btnH  + (cancelBtn?.frame.size.height)! + marginBottom
        
        
    }
    
    func creatHeadView() {
        
        if showHeadView {
            let headtitle = UILabel.setDetailsLab("标题", fontSize: 14, titleColor: LDFontColor.LightGray, Alignment: .Center)
            headtitle.frame = CGRectMake(0, LDFrame.ScreenHeight - originSize.height, LDFrame.ScreenWidth, headViewH)
            //            addSubview(headtitle!)
            originSize.height += headViewH//originSize.height //CGFloat(count)*btnH + LDMargin_V*2 + (cancelBtn?.frame.size.height)!
            setNeedsDisplay()
        }
        
    }
    
    func showview(target:UIView) { //target nav.view
        target.addSubview(gampView!)
        target.addSubview(self)
        
        UIView.animateWithDuration(0.5) {
            self.frame = CGRectMake(0, LDFrame.ScreenHeight - self.originSize.height,LDFrame.ScreenWidth, self.originSize.height)
        }
    }
    
    func dismissview() {
        
        UIView.animateWithDuration(0.5, animations: {
            self.frame.origin.y = LDFrame.ScreenHeight
        }) { (finished) in
            
            //            if self.tapClosure != nil {
            //                self.tapClosure!(title: "取消")
            //            }
            self.gampView?.removeFromSuperview()
            self.removeFromSuperview()
            
        }
    }
    /**选择回调*/
    func selectOfRows(cloure:(title:String,index:Int)->()) {
        self.tapClosure = cloure
    }
    
}